package com.tyl.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockDemo {
    public static void main(String[] args) {
        Phone2 phone=new Phone2();
        new Thread(()->{
            phone.sms();
        },"钥匙1开").start();

        new Thread(()->{
            phone.sms();
        },"钥匙2开").start();
    }
}

class Phone2{
    Lock lock=new ReentrantLock();
    public synchronized void sms(){
        lock.lock();
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"大门");
            call();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
            lock.unlock();
        }
    }
    public synchronized void call(){
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"卧室门");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}